package com.mhkj.base.functionOffice.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.base.functionOffice.dao.FunctionOfficeRepository;
import com.mhkj.base.functionOffice.entity.FunctionOffice;
import com.mhkj.base.functionOffice.service.FunctionOfficeService;
import com.mhkj.base.rbac.role.entity.SystemRole;
import com.mhkj.base.rbac.role.service.MyRoleService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.repository.BaseRepository;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 职能部门
 * @author mics
 * @date 2018年9月17日
 * @version 1.0
 */
@Service
public class FunctionOfficeServiceImpl extends BaseServiceImpl<FunctionOffice> implements FunctionOfficeService{
	@Resource
	private FunctionOfficeRepository functionOfficeRepository;
	@Resource
	private OrgService orgService;
	@Resource
	private MyRoleService myRoleService;
	
	@Resource
	public void setBaseRepository(BaseRepository<FunctionOffice> baseRepository) {
		super.setBaseRepository(functionOfficeRepository);
	}


	@Override
	public Pagination<FunctionOffice> listByPage(FunctionOffice entity,
			int pageIndex, int pageSize) {
		Specification<FunctionOffice> spec = new Specification<FunctionOffice>() {
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<FunctionOffice> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				if (!StringUtils.isEmpty(entity.getName())) {
					lstPredicates.add(criteriaBuilder.like(root.get("name").as(String.class),
							"%" + entity.getName() + "%"));
				}
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		PageRequest pageable = PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id");
		Page<FunctionOffice> pageT = functionOfficeRepository.findAll(spec, pageable);
		Pagination<FunctionOffice> pagination = new Pagination<FunctionOffice>();
		pagination.setTotal(functionOfficeRepository.count(spec));
		List<FunctionOffice> functionOffices = pageT.getContent();
		for(FunctionOffice functionOffice:functionOffices){
			Employee  employee = orgService.employeeInfo(functionOffice.getCreateBy());
			functionOffice.setCreateName(employee.getName());
		}
		pagination.setData(functionOffices);
		return pagination;
	}


	@Override
	@Transactional
	public void grantRole(FunctionOffice functionOffice, String roleIds) {
		FunctionOffice functionTemp = getById(functionOffice.getId());
		String[] roleIdsTemp = roleIds.split(",");
		List<SystemRole> systemRoles = new ArrayList<SystemRole>();
		for(String rid:roleIdsTemp){
			if(StringUtils.hasText(rid)){
				SystemRole systemRole = myRoleService.getById(Long.valueOf(rid));
				systemRoles.add(systemRole);
			}
		}
		functionTemp.setSystemRoles(systemRoles);
		edit(functionTemp);		
	}

}
